

cap program drop DefinePolicyGeos
program define DefinePolicyGeos
syntax using/, FDATE(string) TRTDATE(string) COMPANIES(string)

* Aggregate 
preserve

	* Load file with all postings misssing and non-missing wages
	clear
	use `"`using'"'
		
	qui keep if inrange(mdate,tm(`fdate'),tm(`trtdate')-1)
	di in yellow _n "Pretreatment period"
	tab mdate, m
	return list
	
	qui gen policy_firm=0
	foreach firm in `companies' {
		qui replace policy_firm=1 if company=="`firm'"
	}
	tab company policy_firm, m
	
	encode fips_str, gen(fips)
	distinct fips
	noi di in yellow _n "  Deleting geos without policy firms: " _cont
	keep if policy_firm==1
	distinct fips
	
	qui gen policy_county=1	
	
	collapse (sum) policy_county, by(fips_str)
	
	qui count if policy_county==.
	if `r(N)'!=0 exit 1
	
	qui replace policy_county=1 if policy_county>=1 & policy_county!=.
	qui CheckMiss
	qui compress
	ChkSrt fips_str
	tempfile tmp
	qui save `tmp'
		
restore

qui compress
sort fips_str
merge m:1 fips_str using `tmp'
macro drop _tmp

noi di in yellow _n "  Dropping geos that have postings by policy firms, " _n "  but no non-policy firm postings (_merge==2): " _cont
drop if _merge==2

noi di in yellow _n "  Non-policy geos are _merge==1: " _cont
replace policy_county=0 if _merge==1
drop _merge

encode fips_str, gen(fips)
noi di in yellow _n "  Remaining geos: " _cont
distinct fips

preserve
	qui keep if policy_county==1
	noi di in yellow _n "  Remaining counties with policy firms: " _cont
	distinct fips
restore

preserve
	qui keep if policy_county==0
	noi di in yellow _n "  Remaining counties without policy firms: " _cont
	distinct fips
restore


drop fips
	
end

